package br.com.consultsystems.persistencia.impl.cadastro;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import br.com.consultsystems.dominio.cadastro.Condominio;
import br.com.consultsystems.persistencia.spec.AbstractAdministradoraRepository;
import br.com.consultsystems.persistencia.spec.cadastro.CondominioPersistencia;

@Stateless(name = "condominioPersistencia")
public class CondominioPersistenciaImpl extends
		AbstractAdministradoraRepository<Condominio> implements
		CondominioPersistencia {

	protected CondominioPersistenciaImpl() {
		super(Condominio.class);
	}

	@Override
	public List<Condominio> retrieveAll() {
		return retrieveByCriteria(null);
	}

	@Override
	public List<Condominio> retrieveByFilter(String nome) throws Exception {

		final EntityManager em = getEntityManager();
		final CriteriaBuilder criteriaBuilder = createCriteriaBuilder();
		final CriteriaQuery<Condominio> criteriaQuery = createCriteriaQuery(criteriaBuilder);
		final Root<Condominio> root = createRoot(criteriaQuery);
		
		/* Repository -> Apenas pesquias as informações no banco de dados
		 * Filtro por codigo */
		final Path<String> pathEmail = root.get("nome");
		final Predicate predicateEmail = criteriaBuilder.like(pathEmail, "%"+nome+"%");
		criteriaQuery.where(predicateEmail);
		criteriaQuery.orderBy(criteriaBuilder.asc(pathEmail));
		
		final TypedQuery<Condominio> typedQuery = em.createQuery(criteriaQuery);
		
		return typedQuery.getResultList();
	}

}
